package com.shujia.flink.core

import org.apache.flink.streaming.api.scala._

object Demo2StreamWC {
  def main(args: Array[String]): Unit = {
    /**
      * 构建流处理环境
      *
      */

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    //设置任务并行度
    env.setParallelism(1)

    /**
      *
      * 读取数据
      *
      *
      * 冲socket中读取数据
      *
      * 安装nc  yum install nc
      *
      * 启动socket
      * nc -lk 8888
      *
      *
      */


    //flink读取socket中的数据
    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)


    //将单词拆分开
    val wordsDS: DataStream[String] = linesDS.flatMap(_.split(","))


    //转换成KV格式
    val kvDS: DataStream[(String, Int)] = wordsDS.map(word => (word, 1))



    //统计单词的数量


    //将同一个单词分到同一个分区中
    val keyByDs: KeyedStream[(String, Int), String] = kvDS.keyBy(_._1)

    //对单词求和
    val countDS: DataStream[(String, Int)] = keyByDs.sum(1)


    //打印结果
    countDS.print()


    //启动flink流程序
    env.execute()

  }
}
